package com.lanyuan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    private static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    private static final String PATTERN_DATE = "yyyy-MM-dd";

    /**
     * 格式化日期，包含时间部分
     */
    public static String formatDateTime(Date date) {
        return new SimpleDateFormat(PATTERN_DATE_TIME).format(date);
    }

    /**
     * 格式化日期，包含时间部分 - 现在的时间
     */
    public static String formatDateTime() {
        return new SimpleDateFormat(PATTERN_DATE_TIME).format(new Date());
    }

    /**
     * 格式化日期，仅包含日期部分
     */
    public static String formatDate(Date date) {
        return new SimpleDateFormat(PATTERN_DATE).format(date);
    }

    /**
     * 格式化日期，仅包含日期部分 - 现在的时间
     */
    public static String formatDate() {
        return new SimpleDateFormat(PATTERN_DATE).format(new Date());
    }

    /**
     * 当前日期
     */
    public static String getDate() {
        return getDate(0);
    }

    /**
     * 根据offset返回日期
     */
    public static String getDate(int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, offset);
        return formatDate(calendar.getTime());
    }

    /**
     * 某月最后一天
     */
    public static String getLastDayOfMonth(String year, String month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1); // 先把日期设置成1号以免在修改月份分之后日期值不合法导致未知结果
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return formatDate(calendar.getTime());
    }

    public static String endOfDay(String dateStr) {
        return dateStr + " 23:59:59";
    }

    public static String beginningOfDay(String dateStr) {
        return dateStr + " 00:00:00";
    }

    public static String datePart(String dateTimeStr) {
        return dateTimeStr.substring(0, 10);
    }

    public static Date parseDate(String dateStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_DATE);
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (Exception e) {
        }
        return date;
    }


    /**
     * 获取当前周的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
            cal.set(cal.DAY_OF_WEEK, cal.MONDAY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cal.getTime();
    }

    /**
     * 获取当前周最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
            cal.set(cal.DAY_OF_WEEK, cal.MONDAY);
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 6);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return cal.getTime();
    }

    /**
     * 获取本月开始日期
     * @return String
     * **/
    public static Date getMonthStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static Date getMonthEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
    /**
     * 获取两个日期之间的日期
     * @param startTime
     * @param endTime
     * @return
     */
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)

            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

}
